package com.ruitu.arad.api.okhttp.builder;

import android.content.Context;

import com.ruitu.arad.api.okhttp.MyOkHttp;
import com.ruitu.arad.api.okhttp.callback.MyCallback;
import com.ruitu.arad.api.okhttp.response.IResponseHandler;
import com.ruitu.arad.api.okhttp.util.LogUtils;

import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * delete builder
 * Created by tsy on 2016/12/6.
 */
public class DeleteBuilder extends OkHttpRequestBuilderHasParam<DeleteBuilder> {

    public DeleteBuilder(MyOkHttp myOkHttp) {
        super(myOkHttp);
    }

    @Override
    public void enqueue(Context context, final IResponseHandler responseHandler) {
        try {
            if (mUrl == null || mUrl.length() == 0) {
                throw new IllegalArgumentException("url can not be null !");
            }

            Request.Builder builder = new Request.Builder().url(mUrl);
            appendHeaders(builder, context);

            if (mTag != null) {
                builder.tag(mTag);
            }
            if (mJsonParams.length() > 0) {      //上传json格式参数
                RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), mJsonParams);
                builder.delete(body);

            } else {
                builder.delete(RequestBody.create(MediaType.parse("text/plain;charset=utf-8"), ""));
            }
            Request request = builder.build();

            mMyOkHttp.getOkHttpClient()
                    .newCall(request)
                    .enqueue(new MyCallback(context, responseHandler));
        } catch (Exception e) {
            LogUtils.e("Delete enqueue error:" + e.getMessage());
            responseHandler.onFailure(0, e.getMessage());
        }
    }
}